package com.ruike.alisurface.service;

import android.app.IntentService;
import android.content.Intent;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.serialport.MsgEventBus;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_http.ApiTools;
import com.voodoo.lib_http.DownloadListener;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.FileUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * logo图片下载类
 */
public class MyFileOtherService extends IntentService implements DownloadListener {

    public MyFileOtherService() {
        super("MyFileOtherService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    String logo_img;

    @Override
    protected void onHandleIntent(Intent intent) {
        strpicList.clear();
        try {
            logo_img = intent.getStringExtra("logo_img");
            assert logo_img != null;
            L.i("-----downOtherFile-开始解析数据--logo_img-" + logo_img.toString());
            showImgList();
        } catch (Exception e) {
            L.i("-----downOtherFile-Exception-- " + e.toString());
        }
    }

    ArrayList<String> strpicList = new ArrayList<String>();


    String filepath = Constant.FilePath.ICON_PATH;

    /**
     * 下载 广告图片
     */
    public void showImgList() {
        try {
            if (!logo_img.equals("") && logo_img != null && !logo_img.equals("null")) {
                String Name = logo_img.substring(logo_img.lastIndexOf("/")).substring(1);
                strpicList.add(Name);
            }
            doImgdownload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 进行图片的本地判断和下载
     */
    public void doImgdownload() {
        File myFile = new File(filepath, strpicList.get(0));
        File p = myFile.getParentFile();
        if (!p.exists()) {
            p.mkdirs();
        }
        if (!myFile.exists()) {
            downImgFile(logo_img);
        } else {
            L.i("-----downOtherFile 图片已存在---");
            delFile(strpicList);
        }
    }


    //下载ship
    public void downImgFile(final String strNameList) {
        try {
            L.i("-----downOtherFile---下载图片");
            String Name = strNameList.substring(strNameList.lastIndexOf("/")).substring(1);
            ApiTools.getInstance().getInstance().download(strNameList, filepath, Name, this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void delFile(List<String> strpicList) {
        List<File> strlist = new ArrayList<>();
        strlist.clear();
        File[] folderFileList = FileUtils.getFolderFileList(filepath);
        L.i("tjf-downOtherFile-delect--" + strpicList.toString());
        for (File file : folderFileList) {
            if (!strpicList.contains(file.getName())) {
                strlist.add(file);
            }
        }
        L.i("tjf-downOtherFile-delect2222-" + strlist.toString());
        for (File file : strlist) {
            L.i(file.getName() + "-----删除文件---");
            if (file.exists()) {
                file.delete();
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void onDownloadStart(String url, int id) {

    }

    @Override
    public void onDownloading(String url, long progress, long total, int id) {
        if (progress == 100) {
            L.i("下载图片- 可以下载 " + url + "===" + total + "==" + progress);
        }
    }

    @Override
    public void onDownloadComplete(String url, int id) {

    }

    @Override
    public void onDownloadSuccess(String url, File downloadFile, int id) {
        if (logo_img.contains(url)) {
            L.i("---downOtherFile-图片下载成功 ----" + downloadFile.getAbsolutePath());
            EventBus.getDefault().post(new MsgEventBus(ShareKey.LOGOIMG, "logo成功"));
            delFile(strpicList);
        }
    }

    @Override
    public void onDownloadFail(String url, String errorMsg, int id) {
        L.i("--downOtherFile-图片下载失败-Exception-requestUrl--" + url
                + "======" + errorMsg);
        doImgdownload();
    }


}